﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.AppCode.Enums;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Helpers;

namespace Rio.Framework.Pages.Controls
{
    public partial class BindableOrganizationFolderTypePanel
    {
        public string OrganizationLabel
        {
            get { return (string)GetValue(OrganizationLabelProperty); }
            set { SetValue(OrganizationLabelProperty, value); }
        }
        public static readonly DependencyProperty OrganizationLabelProperty =
            DependencyProperty.Register("OrganizationLabel", typeof(string), typeof(BindableOrganizationFolderTypePanel), null);

        public string FolderTypeLabel
        {
            get { return (string)GetValue(FolderTypeLabelProperty); }
            set { SetValue(FolderTypeLabelProperty, value); }
        }
        public static readonly DependencyProperty FolderTypeLabelProperty =
            DependencyProperty.Register("FolderTypeLabel", typeof(string), typeof(BindableOrganizationFolderTypePanel), null);

        public bool UseDefaultValuesForUser
        {
            get { return (bool)GetValue(UseDefaultValuesForUserProperty); }
            set { SetValue(UseDefaultValuesForUserProperty, value); }
        }
        public static readonly DependencyProperty UseDefaultValuesForUserProperty =
            DependencyProperty.Register("UseDefaultValuesForUser", typeof(bool), typeof(BindableOrganizationFolderTypePanel), null);

        public BindableOrganizationFolderTypePanel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Loaded event of the organizationTypePanelBase control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        public void organizationTypePanelBase_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeOrganizations();

            OrganizationLabel = String.IsNullOrWhiteSpace(OrganizationLabel) ? Resx.GetLabel(ResxCommon.ORGANIZATION) : OrganizationLabel;
            FolderTypeLabel = String.IsNullOrWhiteSpace(FolderTypeLabel) ? Resx.GetLabel(ResxCommon.FOLDER_TYPE) : FolderTypeLabel;
        }

        /// <summary>
        /// Initializes the organizations.
        /// </summary>
        private void InitializeOrganizations()
        {
            Organizations = new ObservableCollection<Organization>();

            OrganizationComparer L_oOrganizationComparer = new OrganizationComparer();

            foreach (Group g in GlobalWebContext.CurrentUser.GroupList)
            {
                foreach (Organization org in g.OrganizationList)
                {
                    if (!Organizations.Contains(org, L_oOrganizationComparer))
                    {
                        if (GlobalWebContext.OrganizationHaveFolderTypeRight(org, Mode))
                        {
                            Organizations.Add(org);
                        }
                    }
                }
            }

            if (SelectedOrganization == null && UseDefaultValuesForUser)
            {
                SelectedOrganization = (from organization in Organizations
                                        where organization.Identifier == GlobalWebContext.CurrentUser.CTX_DefaultOrganization
                                        select organization).SingleOrDefault();
            }

            OnPropertyChanged("SelectedOrganization");

            cbOrganization.SelectedItem = SelectedOrganization;
        }

        /// <summary>
        /// Handles the SelectionChanged event of the ComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void OrganizationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedOrganization = cbOrganization.SelectedItem as Organization;

            FolderTypes = new ObservableCollection<FolderType>();

            FolderTypeComparer L_oFolderTypeComparer = new FolderTypeComparer();

            if (SelectedOrganization != null)
                foreach (FolderType FolderTypeInOrga in SelectedOrganization.FolderTypesList)
                {
                    foreach (Group UserGroup in GlobalWebContext.CurrentUser.GroupList)
                    {
                        if (Mode == AppDisplayMode.Search)
                        {
                            if ((UserGroup.FolderTypeRigthList.Contains(FolderTypeInOrga, L_oFolderTypeComparer)) && ((UserGroup.CanReorganize || UserGroup.CanConsult)))
                            {
                                if (!FolderTypes.Contains(FolderTypeInOrga))
                                {
                                    FolderTypes.Add(FolderTypeInOrga);
                                }
                            }
                        }
                        else if (Mode == AppDisplayMode.Import || Mode == AppDisplayMode.Flow)
                        {
                            if ((UserGroup.FolderTypeRigthList.Contains(FolderTypeInOrga, L_oFolderTypeComparer)) && ((UserGroup.CanScann || UserGroup.CanMassImport)))
                            {
                                if (!FolderTypes.Contains(FolderTypeInOrga))
                                {
                                    FolderTypes.Add(FolderTypeInOrga);
                                }
                            }
                        }
                        else if (Mode == AppDisplayMode.Admin)
                        {
                            if ((UserGroup.FolderTypeRigthList.Contains(FolderTypeInOrga, L_oFolderTypeComparer)) && ((UserGroup.IsAdministrator)))
                            {
                                if (!FolderTypes.Contains(FolderTypeInOrga))
                                {
                                    FolderTypes.Add(FolderTypeInOrga);
                                }
                            }
                        }
                    }
                }


            if (SelectedFolderType == null && UseDefaultValuesForUser)
            {
                    SelectedFolderType = (from folderType in FolderTypes
                                          where folderType.Identifier == GlobalWebContext.CurrentUser.CTX_DefaultType
                                          select folderType).SingleOrDefault();
            }

            OnPropertyChanged("SelectedFolderType");

            cbFolderType.SelectedItem = SelectedFolderType;
        }

        private void cbFolderType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedFolderType = cbFolderType.SelectedItem as FolderType;
        }
    }
}
